home *** CD-ROM | disk | FTP | other *** search
/ BCI NET / BCI NET Dec 94.iso / archives / networking / amitcp / httpd.lha / httpd / http_auth.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-06-18  |  2.6 KB  |  104 lines

  1. /*
  2.  * http_auth: authentication
  3.  * 
  4.  * Rob McCool
  5.  * 
  6.  */
  7.  
  8.  
  9. #include "httpd.h"
  10.  
  11. char user[MAX_STRING_LEN];
  12.  
  13.  
  14. void auth_bong(char *s) {
  15.     char errstr[MAX_STRING_LEN];
  16.  
  17. /* debugging */
  18.     if(s) {
  19.         sprintf(errstr,"%s authorization: %s",remote_name,s);
  20.         log_error(errstr);
  21.     }
  22.     if(!strcmp(auth_type,"Basic")) {
  23.         sprintf(errstr,"Basic realm=\"%s\"",auth_name);
  24.         die(AUTH_REQUIRED,errstr,stdout); /* AAAAAAGH stdout */
  25.     }
  26.     else {
  27.         sprintf(errstr,"Unknown authorization method %s",auth_type);
  28.         die(SERVER_ERROR,errstr,stdout); /* AAAAAAAAGH stdout */
  29.     }
  30. }
  31.  
  32. void check_auth(security_data *sec, int m) {
  33. #ifndef NO_SECURITY
  34.     char at[MAX_STRING_LEN];
  35.     char ad[MAX_STRING_LEN];
  36.     char sent_pw[MAX_STRING_LEN];
  37.     char real_pw[MAX_STRING_LEN];
  38.     char t[MAX_STRING_LEN];
  39.     char w[MAX_STRING_LEN];
  40.     register int x;
  41.     int grpstatus;
  42.  
  43.     if((!auth_name) || (!auth_type) || (!auth_pwfile)) {
  44.         char errstr[MAX_STRING_LEN];
  45.  
  46.         sprintf(errstr,
  47. "httpd: authorization required for %s but not configured",sec->d);
  48.         die(SERVER_ERROR,errstr,stdout); /* AAAAAGH stdout */
  49.     }
  50.         
  51.     if(!auth_line[0])
  52.         auth_bong(NULL);
  53.  
  54.     sscanf(auth_line,"%s %s",at,t);
  55.     if(strcmp(at,auth_type))
  56.         auth_bong("type mismatch");
  57.     uudecode(t,(unsigned char *)ad,MAX_STRING_LEN);
  58.     getword(user,ad,':');
  59.     strcpy(sent_pw,ad);
  60.     if(!get_pw(user,real_pw))
  61.         auth_bong("user not found");
  62.     /* anyone know where the prototype for crypt is? */
  63.     if(strcmp(real_pw,(char *)crypt(sent_pw,real_pw))) 
  64.         auth_bong("password mismatch");
  65.     if(auth_grpfile)
  66.         grpstatus = init_group(auth_grpfile);
  67.     else
  68.         grpstatus = 0;
  69.  
  70.     for(x=0;x<sec->num_auth[m];x++) {
  71.         strcpy(t,sec->auth[m][x]);
  72.         getword(w,t,' ');
  73.         if(!strcmp(w,"valid-user"))
  74.             goto found;
  75.         if(!strcmp(w,"user")) {
  76.             while(t[0]) {
  77.                 getword(w,t,' ');
  78.                 if(!strcmp(user,w))
  79.                     goto found;
  80.             }
  81.         }
  82.         else if(!strcmp(w,"group")) {
  83.             if(!grpstatus)
  84.                 auth_bong("group required and grpfile unreadable");
  85.             while(t[0]) {
  86.                 getword(w,t,' ');
  87.                 if(in_group(user,w)) {
  88.                     log_error("allowing group");
  89.                     goto found;
  90.                 }
  91.             }
  92.         }
  93.         else
  94.             auth_bong("require not followed by user or group");
  95.     }
  96.     if(grpstatus) kill_group();
  97.     auth_bong("user denied");
  98.  
  99.   found:
  100.     if(grpstatus)
  101.         kill_group();
  102. #endif
  103. }
  104.